package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;
import cn.edu.scau.cmi.hombio.dao.RoleEmployeeDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleEmployee;

import cn.edu.scau.cmi.hombio.service.RoleEmployeeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for RoleEmployee entities
 * 
 */

@Controller("RoleEmployeeRestController")

public class RoleEmployeeRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeDAO roleEmployeeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeService roleEmployeeService;

	/**
	 * Create a new Employee entity
	 * 
	 */
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newRoleEmployeeEmployee(@PathVariable Integer roleemployee_id, @RequestBody Employee employee) {
		roleEmployeeService.saveRoleEmployeeEmployee(roleemployee_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new Role entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/role", method = RequestMethod.POST)
	@ResponseBody
	public Role newRoleEmployeeRole(@PathVariable Integer roleemployee_id, @RequestBody Role role) {
		roleEmployeeService.saveRoleEmployeeRole(roleemployee_id, role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}

	/**
	* Delete an existing RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleEmployee(@PathVariable Integer roleemployee_id) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id);
		roleEmployeeService.deleteRoleEmployee(roleemployee);
	}

	/**
	* Delete an existing Role entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/role/{role_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleEmployeeRole(@PathVariable Integer roleemployee_id, @PathVariable Integer related_role_id) {
		roleEmployeeService.deleteRoleEmployeeRole(roleemployee_id, related_role_id);
	}

	/**
	* Create a new RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee", method = RequestMethod.POST)
	@ResponseBody
	public RoleEmployee newRoleEmployee(@RequestBody RoleEmployee roleemployee) {
		roleEmployeeService.saveRoleEmployee(roleemployee);
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee.getId());
	}

	/**
	* Select an existing RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}", method = RequestMethod.GET)
	@ResponseBody
	public RoleEmployee loadRoleEmployee(@PathVariable Integer roleemployee_id) {
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id);
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveRoleEmployeeEmployee(@PathVariable Integer roleemployee_id, @RequestBody Employee employee) {
		roleEmployeeService.saveRoleEmployeeEmployee(roleemployee_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Get Employee entity by RoleEmployee
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getRoleEmployeeEmployee(@PathVariable Integer roleemployee_id) {
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id).getEmployee();
	}

	/**
	* Save an existing Role entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/role", method = RequestMethod.PUT)
	@ResponseBody
	public Role saveRoleEmployeeRole(@PathVariable Integer roleemployee_id, @RequestBody Role role) {
		roleEmployeeService.saveRoleEmployeeRole(roleemployee_id, role);
		return roleDAO.findRoleByPrimaryKey(role.getId());
	}

	/**
	* Show all RoleEmployee entities
	* 
	*/
	@RequestMapping(value = "/RoleEmployee", method = RequestMethod.GET)
	@ResponseBody
	public List<RoleEmployee> listRoleEmployees() {
		return new java.util.ArrayList<RoleEmployee>(roleEmployeeService.loadRoleEmployees());
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRoleEmployeeEmployee(@PathVariable Integer roleemployee_id, @PathVariable Integer related_employee_id) {
		roleEmployeeService.deleteRoleEmployeeEmployee(roleemployee_id, related_employee_id);
	}

	/**
	* Save an existing RoleEmployee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee", method = RequestMethod.PUT)
	@ResponseBody
	public RoleEmployee saveRoleEmployee(@RequestBody RoleEmployee roleemployee) {
		roleEmployeeService.saveRoleEmployee(roleemployee);
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee.getId());
	}

	/**
	* Get Role entity by RoleEmployee
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/role", method = RequestMethod.GET)
	@ResponseBody
	public Role getRoleEmployeeRole(@PathVariable Integer roleemployee_id) {
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id).getRole();
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadRoleEmployeeEmployee(@PathVariable Integer roleemployee_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* View an existing Role entity
	* 
	*/
	@RequestMapping(value = "/RoleEmployee/{roleemployee_id}/role/{role_id}", method = RequestMethod.GET)
	@ResponseBody
	public Role loadRoleEmployeeRole(@PathVariable Integer roleemployee_id, @PathVariable Integer related_role_id) {
		Role role = roleDAO.findRoleByPrimaryKey(related_role_id, -1, -1);

		return role;
	}
}